package model

import (
	"github.com/gomodule/redigo/redis"
	"tizhi_front/nspel"
)

type PhysicalFitness struct {
	Id      int64  `gorm:"primary_key;column:id"`
	Subject string `gorm:"column:subject"`
	Name    string `gorm:"column:name"`
	Level   string `gorm:"column:level"`
	Comment string `gorm:"column:comment"`
	IsBasic bool   `gorm:"column:is_basic"`
	LabelId int64  `gorm:"column:label_id"`
	Label   string `gorm:"column:label"`
}

func (*PhysicalFitness) TableName() string {
	return "physical_fitness"
}

type PhysicalFitness0 struct {
	Subject string `gorm:"column:subject"`
	Name    string `gorm:"column:name"`
	Comment string `gorm:"column:comment"`
	LabelId int64  `gorm:"column:label_id"`
	Label   string `gorm:"column:label"`
}

func (*PhysicalFitness0) TableName() string {
	return "physical_fitness"
}

func (p *PhysicalFitness0) SelectBySubject(subject string) (err error) {
	key := "physical_fitness0.subject:" + subject
	err = nspel.RedisGetGob(key, p)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select([]string{"subject", "name", "comment", "label_id", "label"}).Where("subject = ?", subject).First(p).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, p)
	return
}

func (p *PhysicalFitness0) SelectBySubjectLevel(subject string, level string) (err error) {
	key := "physical_fitness0.subject:" + subject + ".level:" + level
	err = nspel.RedisGetGob(key, p)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select([]string{"subject", "name", "comment", "label_id", "label"}).Where("subject = ?", subject).Where("level = ?", level).First(p).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, p)
	return
}

type PhysicalFitness1 struct {
	LabelId int64 `gorm:"column:label_id"`
}

func (*PhysicalFitness1) TableName() string {
	return "physical_fitness"
}

func (p *PhysicalFitness1) SelectBySubject(subject string) (err error) {
	key := "physical_fitness1.subject:" + subject
	err = nspel.RedisGetGob(key, p)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select("label_id").Where("subject = ?", subject).First(p).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, p)
	return
}
